package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.adapters.XinyongxingjiAdapter;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class Xinyongxingji extends BaseActivity {

	TextView mPhone;
	TextView mName;
	TextView mRegisteDate;
	TextView mOrderCount;
	TextView mScore;
	ListView mList;

	String userID = "";

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_xinyong_xingji);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		userID = getIntent().getStringExtra("userID");

		mPhone = (TextView) findViewById(R.id.phone);
		mName = (TextView) findViewById(R.id.name);
		mRegisteDate = (TextView) findViewById(R.id.registerDate);
		mOrderCount = (TextView) findViewById(R.id.orderCount);
		mScore = (TextView) findViewById(R.id.score);
		mList = (ListView) findViewById(R.id.list);

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {
				mDialog.dismiss();

				if (result instanceof Exception) {

				} else {
					try {
						Object[] array = (Object[]) result;
						SoapObject so = (SoapObject) array[0];
						mPhone.setText(Utils.propertyToShow(so.getProperty(1)));
						mName.setText(Utils.propertyToShow(so.getProperty(2)));
						mRegisteDate.setText(Utils.propertyToShow(so
								.getProperty(3)));
						mOrderCount.setText(Utils.propertyToShow(so
								.getProperty(4)));
						mScore.setText(Utils.propertyToShow(so.getProperty(5)));

						SoapObject listSo = (SoapObject) array[1];
						XinyongxingjiAdapter adapter = new XinyongxingjiAdapter(
								Xinyongxingji.this, listSo);
						mList.setAdapter(adapter);
					} catch (Exception e) {
					}
				}
			};

			protected void onPreExecute() {
				mDialog = ProgressDialog.show(Xinyongxingji.this, "", "加载中...");
			};

			@Override
			protected Object doInBackground(Void... params) {
				Object[] array = new Object[2];
				try {
					Object r0 = mWebServiceClient.common_findUserInfo(
							mCertificate, userID);
					Object r1 = mWebServiceClient.common_findUserOrderInfo(
							mCertificate, userID);
					array[0] = r0;
					array[1] = r1;
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}

				return array;
			}

		}.execute();
	}
}
